@IsTest
private class LeadDedupeTest {
	/*
	//shared variables across test methods
	
    static QueueSobject MarketGroupsQueue = [Select QueueId from QueueSobject q where sobjecttype='Lead'  and queue.name= 'Market Groups Queue'];
    static ID leadCustomerCareRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
    static ID accountCustomerCareRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'Local OOR Customer');
 
    // Setup test data
    static Profile p = [select id from profile where name='SFA Sales Rep OOR'];
          
    static User u = new User(alias = 'salesr', email='oorsalesrep@attinteractive.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', username='oorsalesrep@attinteractive.com');
    
    
	//test for Phone numbers in Lead  and account 
	static testMethod void testValidateTestData() {

		// First make sure there are no leads/acounts already in the system
		// that have the phone numbers used for testing
		Set<String> testPhoneNumber= new Set<String>();
		testPhoneNumber.add('510-366-1000');
		testPhoneNumber.add('510-366-1001');
		testPhoneNumber.add('510-366-1002');
		testPhoneNumber.add('510-366-1003');
		testPhoneNumber.add('510-366-1004');
		testPhoneNumber.add('510-366-1005');
		
		System.assertEquals(0, [SELECT count() FROM Account WHERE Phone IN :testPhoneNumber]);
		System.assertEquals(0, [SELECT count() FROM Lead WHERE Phone IN :testPhoneNumber]);

		Set<String> testCompanyName= new Set<String>();
		testCompanyName.add('aamazon.com');
		testCompanyName.add('bmazon.com');
		testCompanyName.add('cmazon.com');
		testCompanyName.add('dmazon.com');
	    testCompanyName.add('emazon.com');
    	testCompanyName.add('fmazon.com');
    					
		System.assertEquals(0, [SELECT count() FROM Account WHERE name IN :testCompanyName]);
		System.assertEquals(0, [SELECT count() FROM Lead WHERE name IN :testCompanyName]);
		
	}
	
	static User CreateUser() {
	User user = new User(alias = 'oor2', email='oorsalesrep2@attinteractive.com', 
            emailencodingkey='UTF-8', lastname='Testing owner', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', AFrontier_Employee_ID__c= '12345' , username='oorsalesrep2@attinteractive.com');
            
    insert user;
    return user;  
	
	}
	static Lead[]  CreateNewLeads() {
		// Seed the database with some leads, and make sure they can
		// be bulk inserted successfully.
			Lead lead1 = new Lead(Zip_Postal_Code__c='94555',LastName='Test1',LeadSource = 'AOL' , Company='Test1 Inc.',Phone='510-366-1001',Email='test1@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead2 = new Lead(Zip_Postal_Code__c='94555',LastName='Test2',LeadSource = 'AOL' , Company='Test2 Inc.',Phone='510-366-1004',Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead3 = new Lead(Zip_Postal_Code__c='94555',LastName='Test3', LeadSource ='AOL' , Company='Test3 Inc.',Phone='510-366-1005',Email='test5@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead[] 	leads = new Lead[] {lead1, lead2, lead3};
			
        // The following code runs as user 'u'			
		System.runAs(u) {	
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('Current Profile: ' + UserInfo.getProfileId()); 		
		    insert leads;
		}    

			Lead[] insertedLeads= [select Isdupe__C from lead where id in (:lead1.id,:lead2.id,:lead3.id)];
	        //make sure these leads are not marked as duplicate
	                	      		                 		               
	        System.assertEquals(false, insertedLeads[0].IsDupe__C);
	        System.assertEquals(false, insertedLeads[1].IsDupe__C);
			System.assertEquals(false, insertedLeads[2].IsDupe__C);
			
	    
	     return leads;
	}
	static Account[]  CreateNewAccounts(){
			//Seed the database with some test accs, 
		Account acc1 = new Account(RecordTypeId=accountCustomerCareRecordTypeID, Name='aamazon.com',Phone='510-366-1006' );
		Account acc2 = new Account(RecordTypeId=accountCustomerCareRecordTypeID, Name='bmazon.com',Phone='510-366-1007' );
		Account[] accs= new Account[] {acc1, acc2};
		insert accs;
		
		return accs;
	}
	
	static testMethod void testUpdateLeads() {
		
		Lead[] leads = CreateNewLeads();
				
		//update the phone numbers..	
		leads[1].Phone = '510-366-1002';
		leads[2].Phone = '510-366-1003';
					
		test.startTest();
		     System.runAs(u) {	
			    update leads;
		     }
		test.stopTest();
		
   		Lead[] insertedLeads= [select Isdupe__C from lead where id in :new Map<Id,Lead>(leads).KeySet()];
	    //make sure these leads are not marked as duplicate		  
		System.assertEquals(false, insertedLeads[0].IsDupe__C);
	    System.assertEquals(false, insertedLeads[1].IsDupe__C);
		System.assertEquals(false, insertedLeads[2].IsDupe__C);
		
	}
	
	static testMethod void testDuplicateLeadPhone() {
		
		Lead[] leads = CreateNewLeads();
		
		Lead dup1 = new Lead(Company='Test4 Inc.', Phone=leads[0].Phone , Zip_Postal_Code__c='94555',LastName='Test4',LeadSource ='AOL',  Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
		
		test.startTest();
		  System.runAs(u) {	
		        insert dup1;
		  }
		test.stopTest();

		//get the current inserted lead  
    	Lead dupeLead= [select Id, DupingLeadID__c, Duplicate_Reason_Desc__c, Isdupe__C from lead where id =:dup1.Id];
    	
    	//get all the leads
        Lead[] originalLeads= [select Id, DupingLeadID__c, Duplicate_Reason_Desc__c, Isdupe__C from lead where id in :new Map<Id,Lead>(leads).KeySet()];
        //duplicate leads	    
	    System.assertEquals(true, originalLeads[0].IsDupe__C);            	      		                 		               
        System.assertEquals(true, dupeLead.IsDupe__C);
        System.assertEquals(dupeLead.DupingLeadID__c,  originalLeads[0].Id);
        System.assertEquals('Phone', dupeLead.Duplicate_Reason_Desc__c);
        
        //make sure other leads are not marked as duplicate
        System.assertEquals(false, originalLeads[1].IsDupe__C);
		System.assertEquals(false, originalLeads[2].IsDupe__C);
		
	}
	static testMethod void testDuplicateLeadName() {
		
		Lead[] leads = CreateNewLeads();
		
		Lead dup1 = new Lead(Company=leads[0].Company, Phone='510-366-1002', Zip_Postal_Code__c='94555',LastName='Test4',LeadSource ='AOL',  Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
		
		test.startTest();
		  System.runAs(u) {	
		        insert dup1;
		  }
		test.stopTest();

		//get the current inserted lead  
    	Lead dupeLead= [select Id, DupingLeadID__c, Duplicate_Reason_Desc__c, Isdupe__C from lead where id =:dup1.Id];
    	
    	//get all the leads
        Lead[] originalLeads= [select Id, DupingLeadID__c, Duplicate_Reason_Desc__c, Isdupe__C from lead where id in :new Map<Id,Lead>(leads).KeySet()];
        //duplicate leads	    
	    System.assertEquals(true, originalLeads[0].IsDupe__C);            	      		                 		               
        System.assertEquals(true, dupeLead.IsDupe__C);
        System.assertEquals(dupeLead.DupingLeadID__c, originalLeads[0].Id);
        System.assertEquals('Name', dupeLead.Duplicate_Reason_Desc__c);
        
        //make sure other leads are not marked as duplicate
        System.assertEquals(false, originalLeads[1].IsDupe__C);
		System.assertEquals(false, originalLeads[2].IsDupe__C);
		
	}
	static testMethod void testDuplicateAccountName() {
		
		Account[] accs = CreateNewAccounts();
		
		Lead dup1 = new Lead(Company=accs[0].Name, Phone='510-366-1002', Zip_Postal_Code__c='94555',LastName='Test4',LeadSource ='AOL',  Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
		
		test.startTest();
		  System.runAs(u) {	
		        insert dup1;		        
		  }
		test.stopTest();

		//get the current inserted lead  
    	Lead dupeLead= [select Id, DupingLeadID__c, DupingAccountID__c, Duplicate_Reason_Desc__c, Isdupe__C from lead where id =:dup1.Id];
            	      		                 		               
        System.assertEquals(true, dupeLead.IsDupe__C);
        System.assertEquals(dupeLead.DupingAccountID__c, accs[0].Id);
        System.assertEquals('Name', dupeLead.Duplicate_Reason_Desc__c);
        
		
	}
	
	static testMethod void testDuplicateAccountPhone() {
		
		Account[] accs = CreateNewAccounts();
		Lead dup1 = new Lead(Zip_Postal_Code__c='94555',LastName='Test10',LeadSource ='AOL', Phone=accs[1].Phone, Company='Test10 Inc.', Email='test10@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
		try {		
		test.startTest();
		  System.runAs(u) {	
		        insert dup1;
		        System.assert(false, 'Insertion succeeded - we expected it to fail.');		        
		  }
		test.stopTest();

		} 
		catch (DmlException e) {
			System.Debug('****' + e.getDmlMessage(0));	
			System.assertEquals(1, e.getNumDml()); //1 error expected
			System.assertEquals(0, e.getDmlIndex(0));
			System.assertEquals(1, e.getDmlFields(0).size());
			System.assertEquals(e.getDmlFields(0)[0], Lead.Phone);
			System.assertNotEquals(-1, e.getDmlMessage(0).indexOf('An account with this Phone Number already exists.'));
		}
		
	}
	
	static testMethod void testLeadImportAssignOwnerFailure()
	{
			// Seed the database with some leads, and make sure they can
			// be bulk inserted successfully.
			Lead lead1 = new Lead(Zip_Postal_Code__c='94555',LastName='Test1',  Company='Test1 Inc.',Phone='510-366-1001',Email='test1@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead2 = new Lead(Zip_Postal_Code__c='94555',LastName='Test2',  Company='Test2 Inc.',Phone='510-366-1004',Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead3 = new Lead(Zip_Postal_Code__c='94555',LastName='Test3',Company='Test3 Inc.',Phone='510-366-1005',Email='test5@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);			
			Lead[] 	leads = new Lead[] {lead1, lead2, lead3};
			try {	
             test.startTest();						
		       insert leads;
		     test.stopTest();  			
			   System.assert(false, 'Insertion succeeded - we expected it to fail.');
			   
		   } 
		   catch (DmlException e) {				
			System.assertEquals(3, e.getNumDml()); //1 error expected
			System.assertEquals(0, e.getDmlIndex(0));
			System.assertEquals(1, e.getDmlFields(0).size());
			System.assertEquals(e.getDmlFields(0)[0], Lead.AFrontier_Employee_ID__c);
			System.assertNotEquals(-1, e.getDmlMessage(0).indexOf('Af Employee Id not found'));
		}
			
	}
	static testMethod void testLeadImportAssignOwnerSuccess()
	{
			// Seed the database with some leads, and make sure they can
			// be bulk inserted successfully.
			User u1 = CreateUser();
			Lead lead1 = new Lead(Zip_Postal_Code__c='94555',LastName='Test1', AFrontier_Employee_ID__c = u1.AFrontier_Employee_ID__c, Company='Test1 Inc.',Phone='510-366-1001',Email='test1@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead2 = new Lead(Zip_Postal_Code__c='94555',LastName='Test2',  AFrontier_Employee_ID__c = u1.AFrontier_Employee_ID__c,Company='Test2 Inc.',Phone='510-366-1004',Email='test4@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
			Lead lead3 = new Lead(Zip_Postal_Code__c='94555',LastName='Test3',  AFrontier_Employee_ID__c = u1.AFrontier_Employee_ID__c, Company='Test3 Inc.',Phone='510-366-1005',Email='test5@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);			
			Lead[] 	leads = new Lead[] {lead1, lead2, lead3};
	 		test.startTest();		
	         insert leads;
	        test.stopTest(); 
	        //test if owner is set to the user with the same afrontier employeeid			
			for (Lead lead : [SELECT OwnerId FROM lead WHERE id in :new Map<Id,Lead>(leads).KeySet()])
			{
				System.assertEquals(lead.OwnerId, u1.Id);
			}
			
	}
	static testMethod void testLeadMoveStandardAddress()
	{
		Lead lead1 = new Lead(LeadSource = 'Salesgenie.com',Street='Test Street' , City='Fremont', State='CA', PostalCode='94555',LastName='Test1',  Company='Test1 Inc.',Phone='510-366-1001',Email='test1@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
		test.startTest();
		  insert lead1;
		test.stopTest(); 
		//get the lead
        Lead insertedLead = [select City,Street,State,PostalCode, Business_Address_L1__c ,City__c,State_Province__c,Zip_Postal_Code__c from lead where id = :lead1.id];
	    //only for sg.com, address fields should get copied..  
	    System.assertEquals(insertedLead.Business_Address_L1__c, insertedLead.Street);
        System.assertEquals(insertedLead.City__c,  insertedLead.City);
        System.assertEquals(insertedLead.State_Province__c, insertedLead.State);
        System.assertEquals(insertedLead.Zip_Postal_Code__c, insertedLead.PostalCode);       	
	}
	
	static testMethod void testConvertLead() {
        // Create dummy lead
        Lead lead1 = new Lead(Zip_Postal_Code__c='94555',LastName='Test1', LeadSource='AOL', Company='Test1 Inc.',Phone='510-366-1001',Email='test1@duptest.com',RecordTypeId=leadCustomerCareRecordTypeID);
        insert lead1;
    
        // Create dummy conversion
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead1.id);
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];       
		lc.setConvertedStatus(convertStatus.MasterLabel); 
        //lc.setConvertedStatus('Pipeline Prospect');
        test.startTest();
           Database.LeadConvertResult lcr = Database.convertLead(lc);
		test.stopTest(); 
        // Make sure conversion was successful
        System.assert(lcr.isSuccess(), 'Convert Lead failed!');

    }
    
    static testMethod void testSearchbyPhoneandName() {
    	
 //   	Account[] accs = CreateNewAccounts();
 
 		Account acc1 = new Account(RecordTypeId=accountCustomerCareRecordTypeID, Name='aamazon.com',CleanPhoneText__c='5103661006' );
		Account acc2 = new Account(RecordTypeId=accountCustomerCareRecordTypeID, Name='bmazon.com',CleanPhoneText__c='5103661007' );
		Account acc3 = new Account(RecordTypeId=accountCustomerCareRecordTypeID, Name='cmazon.com',CleanPhoneText__c='5103661008' );
		Account[] accs= new Account[] {acc1, acc2,acc3};
		insert accs;
 		for (Account acc:accs)
   		{
            System.Debug('***** Accout CleanPhone : ' + acc.CleanPhoneText__c + acc.RecordTypeId);
		}       
        	
    	
    	//search for matching company name
    	string found= AccountService.SearchbyPhoneandName('aamazon.com','5103661000');
    	System.Debug('matching company name *****'+ found);
    	System.assertEquals('name', found);
    	
    	
    	
    	//if company name and phone matched, return phone match!
    	//found= AccountService.SearchbyPhoneandName('bamazon.com','5103661007');
    //	System.Debug('matching both bamazon *****'+ found);
    	//System.assert(found=='phone');
    	
    	//none matches
    	found= AccountService.SearchbyPhoneandName('noname.com','510-366-1000');
    	System.assertEquals('none', found);
    	
    }
    static testMethod void testEmailHelper() {
    	//send single email test 
    	List<String> toEmail =new List<String>{'smuraleedharan@attinteractive.com'};
    	List<String> ccEmail =new List<String>();
    	EmailHelper.SendMessage('test subject',toEmail, ccEmail, 'This is a a test mail sent by unit test, please ignore!', 1);
    	
    	//test email from template
    	Lead[] emailLeads= CreateNewLeads();
    	string templateName='Case SSP';
        EmailHelper.SendMailfromTemplate(emailLeads, templateName );	
    }
 */   
}